package com.bocloud.cmp.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.BeanDetailDao;
import com.bocloud.cmp.entity.resource.DataCenter;
import com.bocloud.cmp.entity.resource.NetworkDevice;
import com.bocloud.cmp.entity.resource.Rack;
import com.bocloud.cmp.entity.resource.Room;
import com.bocloud.cmp.entity.resource.SafetyDevice;
import com.bocloud.cmp.entity.resource.Server;
import com.bocloud.cmp.entity.resource.StorageDevice;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;

@Repository("beanDetailDao")
public class BeanDetailDaoImpl extends BasicDao implements BeanDetailDao {

	@Override
	public DataCenter getDCBean(Long id) throws Exception {
		String sql = "select * from data_center a where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, DataCenter.class);
		if (list.isEmpty()) {
			return null;
		}
		return (DataCenter) list.get(0);
	}

	@Override
	public Room getRoomBean(Long id) throws Exception {
		String sql = "select a.*,b.name dcname from room a left join data_center b on a.dc_id = b.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, Room.class);
		if (list.isEmpty()) {
			return null;
		}
		return (Room) list.get(0);
	}

	@Override
	public Rack getRackBean(Long id) throws Exception {
		String sql = "select a.*,c.name roomname,d.id dc_id,d.name dcname from rack a left join room c on a.room_id = c.id left join data_center d on c.dc_id = d.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, Rack.class);
		if (list.isEmpty()) {
			return null;
		}
		return (Rack) list.get(0);
	}

	@Override
	public Server getServerBean(Long id) throws Exception {
		String sql = "select a.*,b.name rackname,e.id room_id,e.name roomname,f.id dc_id,f.name dcname "
				+ "from server a left join rack b on a.rack_id = b.id left join room e on b.room_id = e.id "
				+ "left join data_center f on e.dc_id = f.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, Server.class);
		if (list.isEmpty()) {
			return null;
		}
		return (Server) list.get(0);
	}

	@Override
	public NetworkDevice getNetworkDeviceBean(Long id) throws Exception {
		String sql = "select a.*,b.name rackname,e.id room_id,e.name roomname,f.id dc_id,f.name dcname "
				+ "from network_device a left join rack b on a.rack_id = b.id "
				+ "left join room e on b.room_id = e.id left join data_center f on e.dc_id = f.id "
				+ "where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, NetworkDevice.class);
		if (list.isEmpty()) {
			return null;
		}
		return (NetworkDevice) list.get(0);
	}

	@Override
	public StorageDevice getStorageDeviceBean(Long id) throws Exception {
		String sql = "select a.*,b.name rackname,e.id room_id,e.name roomname,f.id dc_id,f.name dcname "
				+ "from storage_device a left join rack b on a.rack_id = b.id "
				+ "left join room e on b.room_id = e.id left join data_center f on e.dc_id = f.id "
				+ "where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, StorageDevice.class);
		if (list.isEmpty()) {
			return null;
		}
		return (StorageDevice) list.get(0);
	}

	@Override
	public SafetyDevice getSafetyDeviceBean(Long id) throws Exception {
		String sql = "select a.*,b.name rackname,e.id room_id,e.name roomname,f.id dc_id,f.name dcname "
				+ "from safety_device a left join rack b on a.rack_id = b.id "
				+ "left join room e on b.room_id = e.id left join data_center f on e.dc_id = f.id "
				+ "where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, SafetyDevice.class);
		if (list.isEmpty()) {
			return null;
		}
		return (SafetyDevice) list.get(0);
	}
}
